<template>
    <div>
        <div
            class="demo-upload-list"
            v-for="item in upload_list"
        >
            <img :src="item">
            <div class="demo-upload-list-cover">
                <Icon
                    type="ios-eye-outline"
                    @click.native="handleView(item)"
                ></Icon>
                <Icon
                    type="ios-trash-outline"
                    @click.native="handleRemove(item)"
                ></Icon>
            </div>
        </div>
        <Progress
            v-if="showProgress"
            :percent="percentage"
            hide-info
        ></Progress>
        <Upload
            ref="upload"
            :show-upload-list="false"
            :on-success="handleSuccess"
            :on-progress="handleProgress"
            :format="['jpg','jpeg','png']"
            :max-size="2048"
            :data="uploadData"
            :on-format-error="handleFormatError"
            :on-exceeded-size="handleMaxSize"
            type="drag"
            :action="uploadUrl"
            style="display: inline-block;width:58px;"
        >
            <div style="width: 58px;height:58px;line-height: 58px;">
                <Icon
                    type="ios-camera"
                    size="20"
                ></Icon>
            </div>
        </Upload>
        <Modal
            footer-hide
            v-model="visible"
        >
            <img
                :src="imgName"
                v-if="visible"
                style="width: 100%"
            >
        </Modal>
    </div>
</template>
<script>
export default {
    props: {
        default_list: {
            type: Array,
            default: []
        }
    },
    data() {
        return {
            // 上传
            showProgress: false,
            percentage: 0,
            uploadUrl: 'http://up-z2.qiniup.com',
            uploadData: {},
            qiniu_url_prefix: '',
            upload_list: this.default_list,
            visible: false,
        }
    },
    created() {
        // 获取七牛token
        this.getQiniuToken()

    },
    watch: {
        default_list(newValue) {
            this.upload_list = newValue
        },
        upload_list(val) {
            this.$emit('uploaded', val)
        }
    },
    methods: {
        // 获取七牛token
        getQiniuToken() {
            let url = 'admin/common/upload/qiniu/token'
            this.$ajax.post(url)
                .then(res => {
                    let data = res.data
                    if (!data.error_code) {
                        this.uploadData = {
                            token: data.data.token,
                            // key: data.data.key
                        }
                        this.qiniu_url_prefix = data.data.qiniu_url_prefix
                    } else {
                        this.$Message.error(data.message);
                    }

                }).catch(err => {
                    console.log(err)
                })
        },
        handleView(name) {
            this.imgName = name;
            this.visible = true;
        },
        handleRemove(file) {
            this.upload_list.splice(this.upload_list.indexOf(file), 1);
        },
        handleSuccess(res, file) {
            let img = this.qiniu_url_prefix + res.key
            this.upload_list.push(img)
            this.showProgress = false
        },
        handleProgress(event, file, fileList) {
            this.showProgress = true
            this.percentage = event.percent

        },
        handleFormatError(file) {
            this.$Notice.warning({
                title: '格式错误',
                desc: '上传文件 ' + file.name + ' 不正确, 请选择 jpg jpeg png.'
            });
        },
        handleMaxSize(file) {
            this.$Notice.warning({
                title: '文件大小限制',
                desc: '文件  ' + file.name + ' 太大了, 不能超过 2M.'
            });
        },
    }
}
</script>
<style>
.avatar-column {
    padding: 10px;
}

.demo-upload-list {
    display: inline-block;
    width: 60px;
    height: 60px;
    text-align: center;
    line-height: 60px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
    margin-right: 4px;
}

.demo-upload-list img {
    width: 100%;
    height: 100%;
}

.demo-upload-list-cover {
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, .6);
}

.demo-upload-list:hover .demo-upload-list-cover {
    display: block;
}

.demo-upload-list-cover i {
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
}
</style>